package com.zhou.jiancai.fragment.tablayoutFragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.RecyclablePagerAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.ColumnLayoutHelper;
import com.alibaba.android.vlayout.layout.FixLayoutHelper;
import com.alibaba.android.vlayout.layout.FloatLayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.alibaba.android.vlayout.layout.OnePlusNLayoutHelper;
import com.alibaba.android.vlayout.layout.RangeGridLayoutHelper;
import com.alibaba.android.vlayout.layout.ScrollFixLayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;
import com.alibaba.android.vlayout.layout.StaggeredGridLayoutHelper;
import com.alibaba.android.vlayout.layout.StickyLayoutHelper;
import com.zhou.jiancai.R;
import com.zhou.jiancai.Utils.LogUtils;
import com.zhou.jiancai.activity.VlayoutActivity;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/15.
 */
@SuppressLint("ValidFragment")
public class AmusementFragment extends Fragment {

    private static final boolean BANNER_LAYOUT = true;

    private static final boolean FIX_LAYOUT = true;

    private static final boolean LINEAR_LAYOUT = true;

    private static final boolean SINGLE_LAYOUT = true;

    private static final boolean FLOAT_LAYOUT = true;

    private static final boolean ONEN_LAYOUT = true;

    private static final boolean COLUMN_LAYOUT = true;

    private static final boolean GRID_LAYOUT = true;

    private static final boolean STICKY_LAYOUT = true;

    private static final boolean STAGGER_LAYOUT = true;

    private SwipeRefreshLayout mSwipeRefreshLayout;

    private TextView mFirstText;
    private TextView mLastText;

    private TextView mCountText;

    private TextView mTotalOffsetText;

    private Runnable trigger;
    private View view1, view2, view3;
    private List<View> viewList;//view数组
    private View view;
    public Activity activity;

    @SuppressLint("ValidFragment")
    public AmusementFragment(Activity activity) {
        activity=activity;
    }

    public  AmusementFragment instance;
    public   AmusementFragment getInstance(){
        if (instance !=null) {
            return instance;

        }else {
            AmusementFragment instance1=new AmusementFragment(getActivity());
            return instance1;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        view = inflater.inflate(R.layout.layout_vlayout, container, false);
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        ;
        mFirstText = (TextView) view.findViewById(R.id.first);
        mLastText = (TextView) view.findViewById(R.id.last);
        mCountText = (TextView) view.findViewById(R.id.count);
        mTotalOffsetText = (TextView) view.findViewById(R.id.total_offset);

        final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.main_view);

        view.findViewById(R.id.jump).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText position = (EditText) view.findViewById(R.id.position);
                if (!TextUtils.isEmpty(position.getText())) {
                    try {
                        int pos = Integer.parseInt(position.getText().toString());
                        recyclerView.scrollToPosition(pos);//跳转到指定位置
                    } catch (Exception e) {
                        Log.e("VlayoutActivity", e.getMessage(), e);
                    }
                } else {
                    recyclerView.requestLayout();//如果没有调用requeslayout  刷新layout
                }
            }
        });


        final VirtualLayoutManager layoutManager = new VirtualLayoutManager(activity);

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int scrollState) {

            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int i, int i2) {
                mFirstText.setText("First: " + layoutManager.findFirstVisibleItemPosition());
                mLastText.setText("Existing: " + MainViewHolder.existing + " Created: " + MainViewHolder.createdTimes);
                mCountText.setText("Count: " + recyclerView.getChildCount());
                mTotalOffsetText.setText("Total Offset: " + layoutManager.getOffsetToStart());
            }
        });


        recyclerView.setLayoutManager(layoutManager);


        // layoutManager.setReverseLayout(true);

        RecyclerView.ItemDecoration itemDecoration = new RecyclerView.ItemDecoration() {//item的装饰
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {//获取条目坐标
                int position = ((VirtualLayoutManager.LayoutParams) view.getLayoutParams()).getViewPosition();
                outRect.set(4, 4, 4, 4);
            }
        };


        final RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();//初始化recycleview的尺  默认5个

        recyclerView.setRecycledViewPool(viewPool);//将recycleview添加到池中

        // recyclerView.addItemDecoration(itemDecoration);

        viewPool.setMaxRecycledViews(0, 20);//第一个参数是类型,第二个参数是最大值

        final DelegateAdapter delegateAdapter = new DelegateAdapter(layoutManager, true);//代表的adapter   要求子条目一致

        recyclerView.setAdapter(delegateAdapter);

        final List<DelegateAdapter.Adapter> adapters = new LinkedList<>();

        if (BANNER_LAYOUT) {

           /* LayoutInflater inflater=getLayoutInflater();
            view1 = inflater.inflate(R.layout.layout1, null);
            view2 = inflater.inflate(R.layout.layout2,null);
            view3 = inflater.inflate(R.layout.layout3, null);
            viewList = new ArrayList<View>();// 将要分页显示的View装入数组中
            viewList.add(view1);
            viewList.add(view2);
            viewList.add(view3);*/



            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), new LinearLayoutHelper(), 1) {

                @Override
                public void onViewRecycled(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder) {
                    if (holder.itemView instanceof ViewPager) {
                        //设置viewpager
                        ((ViewPager) holder.itemView).setAdapter(null);


                     /*   new PagerAdapter(new SubAdapter(VlayoutActivity.this, new LinearLayoutHelper(), 1),viewPool) {
                            @Override
                            public int getCount() {
                                return viewList.size();
                            }

                            @Override
                            public boolean isViewFromObject(View view, Object object) {
                                return view ==object;
                            }

                            @Override
                            public Object instantiateItem(ViewGroup container, int position) {
                                container.addView(viewList.get(position));


                                return viewList.get(position);
                            }

                            @Override
                            public void destroyItem(ViewGroup container, int position, Object object) {
                                container.removeView(viewList.get(position));
                            }
                        }
                        */


                    }
                }

                @Override
                public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    if (viewType == 100)
                        return new MainViewHolder(
                                getActivity().getLayoutInflater().inflate(R.layout.view_pager, parent, false));

                    return super.onCreateViewHolder(parent, viewType);
                }

                @Override
                public int getItemViewType(int position) {
                    return 100;
                }//获取view的类型

                @Override
                protected void onBindViewHolderWithOffset(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position, int offsetTotal) {
                    LogUtils.d("holder"+holder+"postition"+position+"offsetTotal"+offsetTotal);

                }

                @Override
                public void onBindViewHolder(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position) {
                    if (holder.itemView instanceof ViewPager) {
                        ViewPager viewPager = (ViewPager) holder.itemView;

                        viewPager.setLayoutParams(new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200));

                        // from position to get adapter
                        viewPager.setAdapter(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.PagerAdapter(this, viewPool));
                        Toast.makeText(getActivity(), "点击了viewpager"+position, Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }

        //{
        //    GridLayoutHelper helper = new GridLayoutHelper(10);
        //    helper.setAspectRatio(4f);
        //    helper.setGap(10);
        //    adapters.add(new SubAdapter(this, helper, 80));
        //}

        if (FLOAT_LAYOUT) {
            FloatLayoutHelper layoutHelper = new FloatLayoutHelper();///可以拖拽的layout
            layoutHelper.setAlignType(FixLayoutHelper.BOTTOM_RIGHT);
            layoutHelper.setDefaultLocation(100, 400);
            VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(250, 150);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper, 1, layoutParams));
            Toast.makeText(getActivity(), "您点击了可拖拽", Toast.LENGTH_SHORT).show();
        }

        if (LINEAR_LAYOUT) {
            LinearLayoutHelper layoutHelper1 = new LinearLayoutHelper();
            layoutHelper1.setAspectRatio(2.0f);
            LinearLayoutHelper layoutHelper2 = new LinearLayoutHelper();
            layoutHelper2.setAspectRatio(4.0f);
            layoutHelper2.setDividerHeight(10);
            layoutHelper2.setMargin(10, 30, 10, 10);
            layoutHelper2.setPadding(10, 30, 10, 10);
            layoutHelper2.setBgColor(0xFFF5A623);//红色
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper1, 1));
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper2, 6) {

                @Override
                public void onBindViewHolder(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position) {
                    if (position % 2 == 0) {
                        VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
                        layoutParams.mAspectRatio = 5;
                        holder.itemView.setLayoutParams(layoutParams);
                    }
                }
            });
        }

        if (STICKY_LAYOUT) {
            StickyLayoutHelper layoutHelper = new StickyLayoutHelper();
            layoutHelper.setOffset(100);
            layoutHelper.setAspectRatio(4);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper, 1, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
        }

        {
            RangeGridLayoutHelper layoutHelper = new RangeGridLayoutHelper(4);
            layoutHelper.setBgColor(Color.GREEN);
            layoutHelper.setWeights(new float[]{20f, 26.665f});
            layoutHelper.setPadding(15, 15, 15, 15);
            layoutHelper.setMargin(15, 15, 15, 15);
            layoutHelper.setHGap(10);
            layoutHelper.setVGap(10);
            RangeGridLayoutHelper.GridRangeStyle rangeStyle = new RangeGridLayoutHelper.GridRangeStyle();
            rangeStyle.setBgColor(Color.RED);
            rangeStyle.setSpanCount(2);
            rangeStyle.setWeights(new float[]{46.665f});
            rangeStyle.setPadding(15, 15, 15, 15);
            rangeStyle.setMargin(15, 15, 15, 15);
            rangeStyle.setHGap(5);
            rangeStyle.setVGap(5);
            layoutHelper.addRangeStyle(4, 7, rangeStyle);
            RangeGridLayoutHelper.GridRangeStyle rangeStyle1 = new RangeGridLayoutHelper.GridRangeStyle();
            rangeStyle1.setBgColor(Color.YELLOW);
            rangeStyle1.setSpanCount(2);
            rangeStyle1.setWeights(new float[]{46.665f});
            rangeStyle1.setPadding(15, 15, 15, 15);
            rangeStyle1.setMargin(15, 15, 15, 15);
            rangeStyle1.setHGap(5);
            rangeStyle1.setVGap(5);
            layoutHelper.addRangeStyle(8, 11, rangeStyle1);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper, 16));

        }

        if (SINGLE_LAYOUT) {
            SingleLayoutHelper layoutHelper = new SingleLayoutHelper();
            layoutHelper.setBgColor(Color.rgb(135, 225, 90));
            layoutHelper.setAspectRatio(4);
            layoutHelper.setMargin(10, 20, 10, 20);
            layoutHelper.setPadding(10, 10, 10, 10);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper, 1, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
        }

        if (COLUMN_LAYOUT) {
            ColumnLayoutHelper layoutHelper = new ColumnLayoutHelper();
            layoutHelper.setBgColor(0xff00f0f0);
            layoutHelper.setWeights(new float[]{40.0f, Float.NaN, 40});
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper, 5) {

                @Override
                public void onBindViewHolder(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position) {
                    if (position == 0) {
                        VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
                        layoutParams.mAspectRatio = 4;
                        holder.itemView.setLayoutParams(layoutParams);
                    } else {
                        VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);
                        layoutParams.mAspectRatio = Float.NaN;
                        holder.itemView.setLayoutParams(layoutParams);
                        Toast.makeText(getActivity(), "您点击了"+position, Toast.LENGTH_SHORT).show();
                    }
                }

            });
        }

        if (ONEN_LAYOUT) {
            OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
            helper.setBgColor(0xff876384);
            helper.setAspectRatio(4.0f);
            helper.setColWeights(new float[]{40f, 45f});
            helper.setMargin(10, 20, 10, 20);
            helper.setPadding(10, 10, 10, 10);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), helper, 2));
        }

        if (ONEN_LAYOUT) {
            OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
            helper.setBgColor(0xffef8ba3);
            helper.setAspectRatio(2.0f);
            helper.setColWeights(new float[]{40f});
            helper.setRowWeight(30f);
            helper.setMargin(10, 20, 10, 20);
            helper.setPadding(10, 10, 10, 10);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), helper, 4) {
                @Override
                public void onBindViewHolder(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position) {
                    super.onBindViewHolder(holder, position);
                    VirtualLayoutManager.LayoutParams lp = (VirtualLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
                    if (position == 0) {
                        lp.rightMargin = 1;
                    } else if (position == 1) {

                    } else if (position == 2) {
                        lp.topMargin = 1;
                        lp.rightMargin = 1;
                    }
                }
            });
        }

        if (ONEN_LAYOUT) {
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), new OnePlusNLayoutHelper(), 0));
            OnePlusNLayoutHelper helper = new OnePlusNLayoutHelper();
            helper.setBgColor(0xff87e543);
            helper.setAspectRatio(1.8f);
            helper.setColWeights(new float[]{33.33f, 50f, 40f});
            helper.setMargin(10, 20, 10, 20);
            helper.setPadding(10, 10, 10, 10);
            VirtualLayoutManager.LayoutParams lp = new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), helper, 3, lp) {
                @Override
                public void onBindViewHolder(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position) {
                    super.onBindViewHolder(holder, position);
                    VirtualLayoutManager.LayoutParams lp = (VirtualLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
                    if (position == 0) {
                        lp.rightMargin = 1;
                    }
                }
            });
        }

        if (COLUMN_LAYOUT) {
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), new ColumnLayoutHelper(), 0));
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), new ColumnLayoutHelper(), 4));
        }

        if (FIX_LAYOUT) {
            FixLayoutHelper layoutHelper = new FixLayoutHelper(10, 10);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper, 0));

            layoutHelper = new FixLayoutHelper(FixLayoutHelper.TOP_RIGHT, 20, 20);

            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper, 1) {//右侧顶部视图
                @Override
                public void onBindViewHolder(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position) {
                    super.onBindViewHolder(holder, position);
                    VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(200, 200);
                    holder.itemView.setLayoutParams(layoutParams);
                    //视图可自定义
                    Toast.makeText(getActivity(), "您点击了右上角的"+position, Toast.LENGTH_SHORT).show();
                }
            });
        }

        if (STICKY_LAYOUT) {
            StickyLayoutHelper layoutHelper = new StickyLayoutHelper(false);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper, 1));
            layoutHelper = new StickyLayoutHelper(false);
            layoutHelper.setOffset(100);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper, 1, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100)));
        }

        if (GRID_LAYOUT) {
            GridLayoutHelper layoutHelper = new GridLayoutHelper(2);
            layoutHelper.setMargin(7, 0, 7, 0);
            layoutHelper.setWeights(new float[]{46.665f});
            layoutHelper.setHGap(3);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper, 2));

            layoutHelper = new GridLayoutHelper(4);
            layoutHelper.setWeights(new float[]{20f, 26.665f});
            layoutHelper.setMargin(7, 0, 7, 0);
            layoutHelper.setHGap(3);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), layoutHelper, 8));
        }


        if (GRID_LAYOUT) {
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), new GridLayoutHelper(4), 0));

            GridLayoutHelper helper = new GridLayoutHelper(4);
            helper.setAspectRatio(4f);
            //helper.setColWeights(new float[]{40, 20, 30, 30});
            // helper.setMargin(0, 10, 0, 10);
            helper.setGap(10);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), helper, 80) {
                @Override
                public void onBindViewHolder(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position) {
                    super.onBindViewHolder(holder, position);
                    VirtualLayoutManager.LayoutParams lp = (VirtualLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
                    // lp.bottomMargin = 1;
                    // lp.rightMargin = 1;
                }
            });
        }

        if (FIX_LAYOUT) {
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), new ScrollFixLayoutHelper(20, 20), 1) {
                @Override
                public void onBindViewHolder(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position) {
                    super.onBindViewHolder(holder, position);
                    VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(200, 200);
                    holder.itemView.setLayoutParams(layoutParams);
                }
            });
        }

        if (LINEAR_LAYOUT)
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), new LinearLayoutHelper(), 10));

        if (GRID_LAYOUT) {
            GridLayoutHelper helper = new GridLayoutHelper(3);
            helper.setMargin(0, 10, 0, 10);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), helper, 3));
        }

        if (STAGGER_LAYOUT) {
            // adapters.add(new SubAdapter(this, new StaggeredGridLayoutHelper(2, 0), 0));
            final StaggeredGridLayoutHelper helper = new StaggeredGridLayoutHelper(2, 10);
            helper.setMargin(20, 10, 10, 10);
            helper.setPadding(10, 10, 20, 10);
            helper.setBgColor(0xFF86345A);
            adapters.add(new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter(getActivity(), helper, 27) {
                @Override
                public void onBindViewHolder(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position) {
                    super.onBindViewHolder(holder, position);
                    VirtualLayoutManager.LayoutParams layoutParams = new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
                    if (position % 2 == 0) {
                        layoutParams.mAspectRatio = 1.0f;
                    } else {
                        layoutParams.height = 340 + position % 7 * 20;
                    }
                    holder.itemView.setLayoutParams(layoutParams);
                }
            });
        }

        if (COLUMN_LAYOUT) {
            // adapters.add(new SubAdapter(this, new ColumnLayoutHelper(), 3));
        }

        if (GRID_LAYOUT) {
            // adapters.add(new SubAdapter(this, new GridLayoutHelper(4), 24));
        }

        delegateAdapter.setAdapters(adapters);



        final Handler mainHandler = new Handler(Looper.getMainLooper());

        trigger = new Runnable() {
            @Override
            public void run() {
                //recyclerView.scrollToPosition(22);
                //recyclerView.getAdapter().notifyDataSetChanged();
                //mainHandler.postDelayed(trigger, 1000);
                //List<DelegateAdapter.Adapter> newAdapters = new ArrayList<>();
                //newAdapters.add((new SubAdapter(VLayoutActivity.this, new ColumnLayoutHelper(), 3)));
                //newAdapters.add((new SubAdapter(VLayoutActivity.this, new GridLayoutHelper(4), 24)));
                //delegateAdapter.addAdapter(0, new SubAdapter(VLayoutActivity.this, new ColumnLayoutHelper(), 3));
                //delegateAdapter.addAdapter(1, new SubAdapter(VLayoutActivity.this, new GridLayoutHelper(4), 24));
                //delegateAdapter.notifyDataSetChanged();
            }
        };


        mainHandler.postDelayed(trigger, 1000);

        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mainHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeRefreshLayout.setRefreshing(false);
                    }
                }, 2000L);
            }
        });
        setListenerToRootView();

        return view;
    }


    boolean isOpened = false;

    public void setListenerToRootView() {
        final View activityRootView = getActivity().getWindow().getDecorView().findViewById(android.R.id.content);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
                if (heightDiff > 100) { // 99% of the time the height diff will be due to a keyboard.
                    if (isOpened == false) {
                        //Do two things, make the view top visible and the editText smaller
                    }
                    isOpened = true;
                } else if (isOpened == true) {
                    isOpened = false;
                    final RecyclerView recyclerView = (RecyclerView)view. findViewById(R.id.main_view);
                    recyclerView.getAdapter().notifyDataSetChanged();
                }
            }
        });
    }

    // RecyclableViewPager

    static class PagerAdapter extends RecyclablePagerAdapter<com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder> {
        public PagerAdapter(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.SubAdapter adapter, RecyclerView.RecycledViewPool pool) {
            super(adapter, pool);
        }

        @Override
        public int getCount() {
            return 6;
        }

        @Override
        public void onBindViewHolder(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder viewHolder, int position) {
            // only vertical
            viewHolder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            ((TextView) viewHolder.itemView.findViewById(R.id.title)).setText("Banner: " + position);
        }

        @Override
        public int getItemViewType(int position) {
            return 0;
        }
    }


    static class SubAdapter extends DelegateAdapter.Adapter<com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder> {

        private Context mContext;

        private LayoutHelper mLayoutHelper;


        private VirtualLayoutManager.LayoutParams mLayoutParams;
        private int mCount = 0;


        public SubAdapter(Context context, LayoutHelper layoutHelper, int count) {
            this(context, layoutHelper, count, new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300));
        }

        public SubAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams) {
            this.mContext = context;
            this.mLayoutHelper = layoutHelper;
            this.mCount = count;
            this.mLayoutParams = layoutParams;
        }

        @Override
        public LayoutHelper onCreateLayoutHelper() {
            return mLayoutHelper;
        }

        @Override
        public com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_vlayout, parent, false));
        }

        @Override
        public void onBindViewHolder(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position) {
            // only vertical
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }


        @Override
        protected void onBindViewHolderWithOffset(com.zhou.jiancai.fragment.tablayoutFragment.AmusementFragment.MainViewHolder holder, int position, int offsetTotal) {
            ((TextView) holder.itemView.findViewById(R.id.title)).setText(Integer.toString(offsetTotal));
        }

        @Override
        public int getItemCount() {
            return mCount;
        }
    }


    static class MainViewHolder extends RecyclerView.ViewHolder {

        public static volatile int existing = 0;
        public static int createdTimes = 0;

        public MainViewHolder(View itemView) {
            super(itemView);
            createdTimes++;
            existing++;
        }

        @Override
        protected void finalize() throws Throwable {
            existing--;
            super.finalize();
        }
    }
}








    /**
     * @author villadora
     */






